<template>
    <!--日期控件-->
    <div>
        <div class="block">
            <span class="demonstration">年-月</span>
            <el-date-picker
              v-model="date" @change ="dateChange"
              type="month"
              placeholder="选择年-月">
            </el-date-picker>
          </div>
        </div>
    </div>
</template>

<script>
    export default{
        data () {
            return {
                date: '',
                formatDate: ''
            }
        },
        methods: {
            dateChange(){

                var dateStr = this.date.toString();
                //格式化(2021-02-01)
                var d = new Date(dateStr); 
                let year = d.getFullYear(); //年
                let month = d.getMonth()+1; //月
                let day = d.getDate(); //日
                let hours = d.getHours() //时 
                let minutes = d.getMinutes() //分 
                let senc= d.getSeconds() //秒
                
                month = (month < 10) ? '0' + month : month = month; 
                day = (day < 10) ? '0' + day : day = day; 
                hours = (hours < 10) ? '0' + hours : hours = hours; 
                minutes = (minutes < 10) ? '0' + minutes : minutes = minutes; 
                senc = (senc < 10) ? '0' + senc : senc = senc;

                this.formatDate = year + '-' + month + '-' + day;

                console.log(this.formatDate)
            }
        }
    }
</script>

<style>
</style>